package liuliu.throughwaring.ui.activity;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import net.tsz.afinal.annotation.view.CodeNote;

import java.util.ArrayList;
import java.util.List;

import liuliu.custom.control.edittext.ImageEditText;
import liuliu.custom.control.edittext.SpinnerEditText;
import liuliu.custom.control.picker.DatePickerDialog;
import liuliu.custom.control.toolbar.TToolbar;
import liuliu.custom.method.Utils;
import liuliu.throughwaring.R;
import liuliu.throughwaring.base.BaseActivity;
import liuliu.throughwaring.model.AlarmModel;
import liuliu.throughwaring.model.CompanyModel;
import liuliu.throughwaring.model.PersonModel;

/**
 * 报警比对处理
 * Created by Administrator on 2016/5/6.
 */
public class WaringDetailActivity extends BaseActivity {
    @CodeNote(id = R.id.touxiang_img_iv)
    ImageView touxiang_img_iv;//报警人头像
    @CodeNote(id = R.id.name_tv)
    TextView name_tv;//报警人名称
    @CodeNote(id = R.id.guanlian_tv)
    TextView guanlian_tv;//关联信息数量
    @CodeNote(id = R.id.idcard_tv)
    TextView idcard_tv;//身份证号码
    @CodeNote(id = R.id.laiyuan_tv)
    TextView laiyuan_tv;//来源
    @CodeNote(id = R.id.waring_type_tv)
    TextView waring_type_tv;//报警类型
    @CodeNote(id = R.id.deal_time_set, click = "onClick")
    SpinnerEditText deal_time_set;//处理时间
    @CodeNote(id = R.id.deal_result_tv)
    ImageEditText deal_result_tv;//处理结果
    @CodeNote(id = R.id.save_btn, click = "onClick")
    Button save_btn;//保存按钮
    @CodeNote(id = R.id.toolbar)
    TToolbar toolbar;
    @CodeNote(id = R.id.save_btn_ll)
    LinearLayout save_btn_ll;
    public static WaringDetailActivity mInstance;
    DatePickerDialog datePickerDialog;
    List<AlarmModel> alarms;
    AlarmModel alarm;
    @CodeNote(id = R.id.details_ll)
    LinearLayout details_ll;//详细信息
    @CodeNote(id = R.id.edit_ll)
    LinearLayout edit_ll;//编辑页面
    @CodeNote(id = R.id.person_deal_result_tv)
    TextView person_deal_result_tv;
    @CodeNote(id = R.id.guanlian_ll, click = "onClick")
    LinearLayout guanlian_ll;
    private int guanlian_size = 0;

    @Override
    public void initViews() {
        setContentView(R.layout.activity_waring_detail);
        mInstance = this;
    }

    @Override
    public void initEvents() {
        toolbar.setLeftOnClick(new TToolbar.LeftOnClickListener() {
            @Override
            public void leftclick() {
                mInstance.finish();
            }
        });
        //查询出需要填写的处理结果的详细信息
        alarms = finalDb.findAllByWhere(AlarmModel.class, "id='" + getIntent().getStringExtra("waring_id") + "'");
        if (alarms.size() > 0) {
            alarm = alarms.get(0);
            initCompany(alarm.getIdCard());
            guanlian_tv.setText(guanlian_size + "");
            name_tv.setText(alarm.getPersonName());
            idcard_tv.setText(alarm.getIdCard());
            laiyuan_tv.setText(alarm.getPersonType());
            waring_type_tv.setText(alarm.getWaringType());
            deal_time_set.setCenterTxt(alarm.getCreateTime());
            if (null == alarm.getHandleTime() || ("").equals(alarm.getHandleTime())) {//编辑页面
                deal_time_set.setCenterTxt(Utils.getTodayFullString());
                details_ll.setVisibility(View.GONE);
                edit_ll.setVisibility(View.VISIBLE);
            } else {//非编辑页面
                details_ll.setVisibility(View.VISIBLE);
                edit_ll.setVisibility(View.GONE);
                deal_time_set.setCenterTxt(alarm.getHandleTime());
            }
            if (null != alarm.getContent() || ("").equals(alarm.getContent())) {
                person_deal_result_tv.setText(alarm.getContent());
            }
        }
    }

    /**
     * 加载关联信息的公司数量
     *
     * @param cardId 证件号码
     */
    private void initCompany(String cardId) {
        List<PersonModel> list = finalDb.findAllByWhere(PersonModel.class, "PersonCardId='" + cardId + "'");
        List<CompanyModel> guList = new ArrayList<CompanyModel>();
        for (PersonModel p : list) {
            List<CompanyModel> clist = finalDb.findAllByWhere(CompanyModel.class, "CompanyId='" + p.getPersonCompanyId() + "'");
            if (clist.size() > 0) {
                Boolean isY = true;
                for (CompanyModel c : guList) {
                    if (c.getCompanyId().equals(clist.get(0).getCompanyId())) {
                        isY = false;
                        break;
                    }
                }
                if (isY) {
                    guList.add(clist.get(0));
                }
            }
        }
        guanlian_size = guList.size();
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.save_btn://保存
                if (deal_result_tv.getText().toString().trim().equals("")) {
                    mUtils.ToastShort("请填写处理原因");
                } else {
                    alarm.setHandleTime(deal_time_set.getCenterText());
                    alarm.setContent(deal_result_tv.getText().toString());
                    finalDb.update(alarm);
                    mUtils.ToastShort("保存成功");
                    this.finish();//关闭页面
                }
                break;
            case R.id.deal_time_set://处理时间
                datePickerDialog = new DatePickerDialog(mInstance, "");
                datePickerDialog.datePickerDialog(deal_time_set);
                break;
            case R.id.guanlian_ll:
                if (guanlian_size > 0) {//关联信息大于0的时候
                    //跳到关联页面
                    mUtils.IntentPost(SearchCompanyListActivity.class, new Utils.putListener() {
                        @Override
                        public void put(Intent intent) {
                            intent.putExtra("CardId", idcard_tv.getText());
                        }
                    });
                }
                break;
        }
    }
}
